#include "board.h"
#include "ad.h"
#include "stm32f4xx.h"
#include "debug.h"

static uint16_t rx_mq2_value;
bool scan_mode_ctr = true;
uint32_t rx_key_value;
uint16_t rx_ad_value;
float voltage = 0;
float current = 0;

void adc_task(void* param)
{
    ad_init();
    mq2_ad_init();
    while(1)
    {
        // 获取mq2传感器值
        rx_mq2_value = mq2_get_value();
        // send oled_task notify value.
        xTaskNotify(oled_task_handle, (uint32_t)rx_mq2_value, eSetValueWithOverwrite);
        // receive key_task notify value.
        xTaskNotifyWait(0, 0xFFFFFFFF, &rx_key_value, pdMS_TO_TICKS(100));

        // verify key_task notify value
        if(rx_key_value & 0x01)
        {
            scan_mode_ctr = false;
            rx_ad_value = adc_get_value();
            voltage = (float)rx_ad_value / 4095.0f * 3.3f;
            // current = voltage / 10;
            // printf("valtage: %.2f\r\n", voltage);
            xQueueSend(extern_to_oled_data, &voltage, pdMS_TO_TICKS(100));
            // xQueueSend(extern_to_oled_data, &current, pdMS_TO_TICKS(100));
        }
        else if(rx_key_value & 0x02)
        {
            scan_mode_ctr = true;
        }

        if(scan_mode_ctr)
        {
            rx_ad_value = adc_get_value();
            voltage = (float)rx_ad_value / 4095.0f * 3.3f;
            // current = voltage / 10;
            xQueueSend(extern_to_oled_data, &voltage, pdMS_TO_TICKS(100));
            // xQueueSend(extern_to_oled_data, &current, pdMS_TO_TICKS(100));
        }
    }
}
